package com.woniuxy.rabbitmq.baseapi.mq08ack;

/**
 * description: Producer <br>
 * date: 2022/1/10 5:05 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */


import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 消费者发送消息
 */
public interface Producer {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //连接工厂：配置连接参数，创建连接对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.13.28");
        //connectionFactory.setHost("127.0.0.1");
        //设置虚拟机主机：类似于mysql数据库服务器中的一个具体数据库
        connectionFactory.setVirtualHost("/");
        //消息通信端口
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //连接对象
        Connection connection = connectionFactory.newConnection();
        //连接信道
        Channel channel = connection.createChannel();

        for (int i = 1; i <= 5; i++) {
            channel.basicPublish("exchangetopic", "email", null, ("email" + i).getBytes(StandardCharsets.UTF_8));

        }
        System.out.println("----------------");
        System.out.println("发送成功");
        //关闭资源
        //使用生产者确认，连接资源不能关
//        channel.close();
//        connection.close();
    }
}
